package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "菜品相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    private final StringRedisTemplate stringRedisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        dishService.save(dishDTO);
        String key="sky:dish:"+dishDTO.getCategoryId();
        stringRedisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("进行菜品分页查询{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品，可批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除编号为{}的菜品",ids);
        dishService.deleteBatch(ids);

        clearCache();

        return Result.success();
    }

    /**
     * 通过id获取菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("获取id为{}的菜品",id);
        DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 菜品的起售停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品的起售停售")
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Long id){
        log.info("修改菜品id为{}的状态为{}",id,status);
        dishService.status(status,id);

        clearCache();

        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息{}",dishDTO);
        dishService.update(dishDTO);

        clearCache();

        return Result.success();
    }

    /**
     * 根据分类id获取菜品
     * @return
     */
    @ApiOperation("根据分类id获取菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        log.info("获取分类{}下的菜品",categoryId);
        Category category = Category.builder()
                .id(categoryId)
                .build();
        List<DishVO> dishVOList = dishService.getByCategoryId(category);
        return Result.success(dishVOList);
    }

    /**
     * 清理缓存数据
     */
    private void clearCache() {
        String key="sky:dish:*";
        Set<String> keys = stringRedisTemplate.keys(key);
        stringRedisTemplate.delete(keys);
    }

}
